package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.view.CheckView;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.conn.JsonShopcartAsyGet;
import com.zcx.qshop.view.CalculatorView;
import com.zcx.qshop.view.SelectView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public abstract class ShopCarAdapter extends QSAdapter<JsonShopcartAsyGet.Info.Shop> implements CalculatorView.OnCountChangeListener {

    private DecimalFormat decimalFormat = new DecimalFormat("0.00");

    private LayoutInflater layoutInflater;

    public ShopCarAdapter(Context context, List<JsonShopcartAsyGet.Info.Shop> objects) {

        super(context, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final JsonShopcartAsyGet.Info.Shop shop = getItem(position);

        ViewHolder viewHolder;

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_shop_car, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.selectView.setOnCheckChangeListener(new CheckView.OnCheckChangeListener() {

            @Override
            public void onCheckChange(View var1, boolean var2) {

                shop.isSelect = var2;

                selectChange();

            }

        });

        viewHolder.img.setImageURI(Uri.parse(shop.picurl));

        viewHolder.selectView.setCheck(shop.isSelect, false);

        viewHolder.name.setText(Html.fromHtml(shop.title + " " + shop.one + " " + shop.two));

        viewHolder.price.setText(decimalFormat.format(shop.price));

        viewHolder.unit.setText("元/" + shop.unit);

        viewHolder.calculatorView.setCount(shop.number + "");

        viewHolder.calculatorView.setOnCountChangeListener(shop, this);

        return convertView;

    }

    public void setSelectAll(boolean isSelectAll){

        for (int i = 0 ; i < getCount() ; i ++){

            getItem(i).isSelect = isSelectAll;

        }

        selectChange();

    }

    public void selectChange(){

        boolean isSelectAll = true;

        double total = 0;

        if(getCount() > 0){

            for (int i = 0 ; i < getCount() ; i ++){

                JsonShopcartAsyGet.Info.Shop shop = getItem(i);

                if(!shop.isSelect){

                    isSelectAll = false;

                    continue;

                }else{

                    total += shop.price * shop.number;

                }

            }

        }else{

            isSelectAll = false;

        }

        onTotalChange(decimalFormat.format(total));

        onSelectAll(isSelectAll);

        notifyDataSetChanged();

    }

    protected abstract void onTotalChange(String total);

    protected abstract void onSelectAll(boolean isSelect);

    private class ViewHolder{

        @BoundView(R.id.item_shop_car_select_view) private SelectView selectView;

        @BoundView(R.id.item_shop_car_img) public SimpleDraweeView img;

        @BoundView(R.id.item_shop_car_name) public TextView name;

        @BoundView(R.id.item_shop_car_price) public TextView price;

        @BoundView(R.id.item_shop_car_unit) public TextView unit;

        @BoundView(R.id.item_shop_car_calculator_view) private CalculatorView calculatorView;

    }

}
